package IT.DataAccess;

import IT.Entities.UsuarioPermiso;
import java.sql.ResultSet;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatUsuarioPermiso implements IDatUsuarioPermiso{
  
    private BDConexion oBase;    
    private List<UsuarioPermiso> oLista;
    private Connection oConexion;
    private CallableStatement oCst;   
    private ResultSet oRst;    
    
    public List<UsuarioPermiso> Listar(int nPerfil) {
        oLista=new ArrayList<UsuarioPermiso>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoListar(?)}");
            oCst.setInt(1, nPerfil);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new UsuarioPermiso(
                        oRst.getInt("cod_mnu_in"),                         
                        oRst.getInt("cod_per_in")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public void Insertar(UsuarioPermiso ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoInsertar(?,?)}");              
            oCst.setInt(1, ent.getCod_mnu_in());
            oCst.setInt(2, ent.getCod_per_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(UsuarioPermiso ent) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoEliminar(?,?)}");              
            oCst.setInt(1, ent.getCod_mnu_in());
            oCst.setInt(2, ent.getCod_per_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void EliminarTodo(int nPerfil) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoTodoEliminar(?)}");              
            oCst.setInt(1, nPerfil);
                        
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<UsuarioPermiso> ListarDetalle() {
        oLista=new ArrayList<UsuarioPermiso>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoDetalleListar()}");     
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new UsuarioPermiso(                        
                        oRst.getInt("cod_per_in"),
                        oRst.getInt("cod_mnu_in"),                         
                        oRst.getInt("tip_perdet_in"),
                        oRst.getString("Tipo"),
                        oRst.getString("nom_mnu_vc")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public List<UsuarioPermiso> UbiDetalle(int nPerfil) {
        oLista=new ArrayList<UsuarioPermiso>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoDetallePerfilUbi(?)}");
            oCst.setInt(1, nPerfil);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new UsuarioPermiso(                                          
                        oRst.getInt("cod_per_in"),
                        oRst.getInt("cod_mnu_in"),      
                        oRst.getInt("tip_perdet_in"),
                        oRst.getString("Tipo")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public void InsertarDetalle(UsuarioPermiso ent) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoDetalleInsertar(?,?,?)}");                          
            oCst.setInt(1, ent.getCod_per_in());
            oCst.setInt(2, ent.getCod_mnu_in());
            oCst.setInt(3, ent.getTip_perdet_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void EliminarDetalle(UsuarioPermiso ent) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoDetalleEliminar(?,?,?)}");              
            oCst.setInt(1, ent.getCod_per_in());
            oCst.setInt(2, ent.getCod_mnu_in());
            oCst.setInt(3, ent.getTip_perdet_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void EliminarTodoDetalle(int nPerfil) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioPermisoDetalleEliminarTodo(?)}");              
            oCst.setInt(1, nPerfil);           
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
